import java.io.*;
import java.util.*;
import java.awt.Font;
import javax.swing.*;
import java.security.*;
import java.math.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ProgramUitl
{
	//Misc constants
	public static final int MINIMUM_AGE=16;
	public static final int MAX_AGE=80;
	public static final String TRUE="true";
	public static final String FALSE="false";
	public static final String DEFAULT_DELIMETER="#";
	public static final String EMPTY=" ";
	public static final int MAX_MARKS=100;
	public static final int MIN_MARKS=0;
	//
	
	//GUI Constants
	public enum ICON_TYPE {
		ERROR,INOFRMATION,NOTICE,QUESTION;
	}
	public static final int SCREEN_HEIGHT=300;
	public static final int SCREEN_WIDTH=400;
	
	public static final int SMALL_SCREEN_HEIGHT=240;
	public static final int SMALL_SCREEN_WIDTH=460;
	public static final int SMALL_SCREEN_WIDTH_2=600;
	
	public static final int MEDIUM_SCREEN_HEIGHT=380;
	public static final int MEDIUM_SCREEN_2_HEIGHT=680;
	public static final int MEDIUM_SCREEN_WIDTH=500;
	public static final int MEDIUM_SCREEN_2_WIDTH=580;
	
	public static final int SCREEN_LOCATION_TOP=280;
	public static final int SCREEN_LOCATION_LEFT=200;
	
	public static final String HEADNING_TEXT="University Grade Management System";
	public static final int HEADING_FONT_SIZE=15;
	public static final int HEADING_FONT_STLYE=1; //Font.BOLD
	
	public static final int NORMAL_FONT_SIZE=12;
	public static final int NORMAL_FONT_STLYE=0; //Font.NORMAL
	
	public static final String FONT_NAME="Arial Black";
	
	public static final String ADD_TEXT="Add";
	public static final String CREATE_TEXT="Create";
	
	public static final String YES_TEXT="Yes";
	public static final String NO_TEXT="No";
	public static final String CLEAR_TEXT="Clear";
	public static final String SEARCH_TEXT="Search";
	
	public static final String ADD_BATCH_TEXT="Create batch";
	public static final String ADD_COURSE_TEXT="Add new Course";
	public static final String ADD_STUDENT_TEXT="Add new student";
	public static final String ENROLE_STUDENT_TEXT="Add Courses to Batch";
	public static final String UPDATE_STUDENT_MARKS_TEXT="Update Student Marks";
	public static final String CALCULATE_GPA_TEXT="View GPA and Class";
	public static final String VIEW_RESULT_SHEET_TEXT="View Result Sheet";	
	public static final String EXIT_TEXT="Exit";
	
	//Batch
	public static final String BATCH_CODE_TEXT="Batch Code";
	public static final String BATCH_DESC_TEXT="Batch Description";
	public static final String BATCH_CONFIRMATION="Do you wish to create ?";
	//
	
	//Course
	public static final String COURSE_ALL_TEXT="All Courses";
	public static final String COURSE_ENROLLED_TEXT="Enrolled Courses";	
	public static final String COURSE_CODE_TEXT="Course Code";
	public static final String COURSE_NAME_TEXT="Course Name";
	public static final String COURSE_TYPE_TEXT="Type";
	public static final String COURSE_CREDITS_TEXT="Credits";
	public static final String COURSE_PRACTICAL="Practicals";
	
	public static final String COURSE_TYPE__CORE_TEXT="Core/Compulsory";
	public static final String COURSE_TYPE__OPTIONAL_TEXT="Optional";
	public static final String COURSE_TYPE__PROJECT_TEXT="Project";
	//
	
	//Student
	public static final String STUDNET_NAME_TEXT="Student Name";
	public static final String STUDNET_ID_TEXT="NIC No";
	public static final String STUDNET_AGE_TEXT="Age";
	public static final String STUDNET_NO_TEXT="Student No";
	public static final String STUDNET_TP_TEXT="Telephone No";
	public static final String STUDNET_ADDRESS_TEXT="Address";
	public static final String STUDNET_BATCH_TEXT="Batch";
	//
	
	//Enrollment
	public static final String ENROLLMENT_ALREADY_ENROLLED_TEXT="Already enrolled courses";
	public static final String ENROLLMENT_NO_COURSE="Please select course(s) to enroll";
	public static final String ENROLLMENT_CONFIRMATION="Do you wish to enroll ?";
	
	//
	public static final String SCCUESS=" successfully !";
	public static final String FAILED=" failed !";
	
	public static final String ADDED_SUCCESS_TEXT=" added"+SCCUESS;
	public static final String ALREADY_EXISTS_TEXT=" already exists !";
		
	public static final String INVALID_FORMAT_ERROR=" is invalid format";
	public static final String REQUIRED_FIELD_ERROR=" must be filled";
	
	public static final String OBJECT_NOT_FOUND="Object not found";
	
	public static final String WISH_TO_PROCEED="Wish to proceed ?";
	
	public static final String EXIT_PRORAM_MSG="Are you sure you want to exit?";	
	//
	
	//Marks
	public static final String MARK_NORMAL_TEXT="Normal/Medical";
	public static final String MARK_REPEAT_TEXT="Repeat";
	
	public static final String MARKS_CONFRIM_TO_CLEAR_TEXT="Wish to clear all the fields ?";
	public static final String MARKS_CONFRIM_TO_ADD_TEXT="Wish to Update marks ?";
	public static final String MARKS_CARD_TYPE="Result Sheet Type";
	public static final String MARKS_CARD_WITH_PRAC="With Practicals";
	public static final String MARKS_CARD_NO_PRAC="With Out Practicals";
	public static final String MARKS_VIEW_MARKS="View Marks Cards";
	
	public static final int DEPTH_WITH_PRAC=8;
	public static final int DEPTH_NO_PRAC=12;	
	
	//
	
	//Core/Optional
	public static final String MARK_ASSIGN_1_TEXT="Assignment 1";
	public static final String MARK_ASSIGN_2_TEXT="Assignment 2";
	public static final String MARK_ASSIGN_3_TEXT="Assignment 3";
	public static final String MARK_CLASS_TEST_TEXT="Class Test";
	public static final String MARK_FINAL_PAPER_TEXT="Final Paper";
	public static final String MARK_PRACTICAL_TEXT="Practical";
	//
	//Project
	public static final String MARK_PROJECT_INTERIM_REPORT_TEXT="Interim report";
	public static final String MARK_PROJECT_INTERIM_PRESENTATION_TEXT="Interim presentation";
	public static final String MARK_PROJECT_FINAL_REPORT_TEXT="Final report";
	public static final String MARK_PROJECT_FINAL_PRESENTATION_TEXT="Final presentation";
	public static final String MARK_PROJECT_FINAL_PRODUCT_TEXT="Final Product";	
	//	
	//
	
	//GPA/Class
	public static final String GPA_GPA_VALUE="GPA";
	public static final String GPA_CLASS_VALUE="Class";
	public static final String GPA_CLASS_NA="N/A";

	//
	
	//XML operations
	//XML Files
	public static final String XML=".xml";
	
	public static final String DIR_SEP="\\";
	public static final String DB_FOLDER="db";
	public static final String ENROLEMENET_FOLDER="enrolments";
	public static final String BATCH_FOLDER="batch";
	
	public static final String DB_PATH=DB_FOLDER+DIR_SEP;
	public static final String STUDENT_XML=DB_PATH+"student"+XML;
	public static final String COURSE_XML=DB_PATH+"course"+XML;
	public static final String ENROLLMENT_XML=DB_PATH+"enroll"+XML;
	public static final String BATCH_XML=DB_PATH+"batch"+XML;
	
	public static final String ENROLEMENET_DB_PATH=DB_PATH+ENROLEMENET_FOLDER+DIR_SEP;
	public static final String BATCH_DB_PATH=DB_PATH+BATCH_FOLDER+DIR_SEP;
	
	//Batch
	public static final String BATCH_XML_ELEMENT_ROOT="batches";
	public static final String BATCH_XML_ELEMENT="batch";
	public static final String BATCH_XML_NO_ELEMENT="no";		
	public static final String BATCH_XML_DESC_ELEMENT="desc";		
	
	//Course
	public static final String COURSE_XML_ELEMENT_ROOT="courses";		
	public static final String COURSE_XML_ELEMENT="course";
	public static final String COURSE_XML_NO_ELEMENT="no";
	public static final String COURSE_XML_NAME_ELEMENT="name";
	public static final String COURSE_XML_TYPE_ELEMENT="type";
	public static final String COURSE_XML_CREDITS_ELEMENT="credits";
	public static final String COURSE_XML_HASPRACTICAL_ELEMENT="practical";
	//
	
	//Students
	public static final String STUDENT_XML_ELEMENT_ROOT="students";
	public static final String STUDENT_XML_ELEMENT="student";
	
	public static final String STUDENT_XML_NO_ELEMENT="no";
	public static final String STUDENT_XML_ID_ELEMENT="id";
	public static final String STUDENT_XML_NAME_ELEMENT="name";
	public static final String STUDENT_XML_AGE_ELEMENT="age";	
	public static final String STUDENT_XML_ADDRESS_ELEMENT="address";
	public static final String STUDENT_XML_TP_ELEMENT="tel";
	public static final String STUDENT_XML_BATCH_ELEMENT="batch";
	//
		
	//Enrollments
	public static final String ENROLL_XML_ELEMENT_ROOT="enrollments";
	public static final String ENROLL_XML_ELEMENT="enrollment";
	
	//public static final String ENROLL_NORMAL_XML_ELEMENT="n";
	//public static final String ENROLL_REPEAT_ELEMENT="r";
	
	public static final String ENROLL_COURSE_NO_XML_ELEMENT="course_no";
	public static final String ENROLL_STUDENT_NO_ELEMENT="student_no";
	
	public static final String ENROLL_MARKS_ASSIGNMENT_1_XML_ELEMENT="a1";
	public static final String ENROLL_MARKS_ASSIGNMENT_2_XML_ELEMENT="a2";
	public static final String ENROLL_MARKS_ASSIGNMENT_3_XML_ELEMENT="a3";	
	public static final String ENROLL_MARKS_CLASS_TEST_XML_ELEMENT="ct";
	public static final String ENROLL_MARKS_PRCTICAL_XML_ELEMENT="p";	
	public static final String ENROLL_MARKS_FINAL_PAPER_XML_ELEMENT="fp";	
	//Enrollment Batch
	
	//
	
	
	//Repeat
	public static final String ENROLL_MARKS_ASSIGNMENT_1_REPEAT_XML_ELEMENT="r_a1";
	public static final String ENROLL_MARKS_ASSIGNMENT_2_REPEAT_XML_ELEMENT="r_a2";
	public static final String ENROLL_MARKS_ASSIGNMENT_3_REPEAT_XML_ELEMENT="r_a3";	
	public static final String ENROLL_MARKS_CLASS_TEST_REPEAT_XML_ELEMENT="r_ct";
	public static final String ENROLL_MARKS_PRCTICAL_REPEAT_XML_ELEMENT="r_p";	
	public static final String ENROLL_MARKS_FINAL_PAPER_REPEAT_XML_ELEMENT="r_fp";
	//
	
	//Project
	public static final String ENROLL_MARKS_PROJECT_INTERIM_REPORT_XML_ELEMENT="p_ir";
	public static final String ENROLL_MARKS_PROJECT_INTERIM_PRESENTATION_XML_ELEMENT="p_ip";
	public static final String ENROLL_MARKS_PROJECT_FINAL_REPORT_XML_ELEMENT="p_fr";
	public static final String ENROLL_MARKS_PROJECT_FINAL_PRESENTATION_XML_ELEMENT="p_fp";	
	public static final String ENROLL_MARKS_PROJECT_FINAL_PRODUCT_XML_ELEMENT="p_fpro";	
	
	//Repeat
	public static final String ENROLL_MARKS_PROJECT_INTERIM_REPORT_REPEAT_XML_ELEMENT="r_p_ir";
	public static final String ENROLL_MARKS_PROJECT_INTERIM_PRESENTATION_REPEAT_XML_ELEMENT="r_p_ip";
	public static final String ENROLL_MARKS_PROJECT_FINAL_REPORT_REPEAT_XML_ELEMENT="r_p_fr";
	public static final String ENROLL_MARKS_PROJECT_FINAL_PRESENTATION_REPEAT_XML_ELEMENT="r_p_fp";	
	public static final String ENROLL_MARKS_PROJECT_FINAL_PRODUCT_REPEAT_XML_ELEMENT="r_p_fpro";		
	//	
	//
	
	public static final String ENROLL_FINAL_MARK_XML_ELEMENT="m_f";
	public static final String ENROLL_FINAL_GRADE_XML_ELEMENT="m_f_g";	
	public static final String ENROLL_FINAL_GP_XML_ELEMENT="m_f_gp";	
	
	//
	
	public static final String XML_STATUS_ELEMENT="status";
	//
	//
	
	public enum ERROR_TYPE {
		ADDED,ALREADY_EXISTS,ERROR;
	}
	public enum COURSE_TYPE{
		CORE,OPTIONAL,PROJECT
	}
	public enum STATUS{
		ENABLED,DISABLED
	}	
	//

	//Grades
	public static final String GRADE_DEFAULT="K";
	
	public static final String GRADE_A_P_TEXT="A+";
	public static final String GRADE_A_TEXT="A";
	public static final String GRADE_A_M_TEXT="A-";
	public static final String GRADE_B_P_TEXT="B+";
	public static final String GRADE_B_TEXT="B+";
	public static final String GRADE_B_M_TEXT="B-";
	public static final String GRADE_C_TEXT="C";
	public static final String GRADE_D_TEXT="D";
	public static final String 	GRADE_E_TEXT="E";
	//GP
	public static final float GRADE_A_P_GP=4.5F;
	public static final float GRADE_A_GP=4F;
	public static final float GRADE_A_M_GP=3.8F;
	public static final float GRADE_B_P_GP=3.3F;
	public static final float GRADE_B_GP=3F;
	public static final float GRADE_B_M_GP=2.8F;
	public static final float GRADE_C_GP=2F;
	public static final float GRADE_D_GP=1F;
	public static final float GRADE_E_GP=0F;
	//
	
	//GPA
	public static final float GPA_FIRST=3.5F;
	public static final float GPA_SECOND_UPPER=3.2F;
	public static final float GPA_SECOND_LOWER=2.9F;
	public static final float GPA_NORMAL=2.0F;
	//
	//Class
	public static final String CLASS_FIRST="First class";
	public static final String CLASS_SECOND_UPPER="Second class Upper Division";
	public static final String CLASS_SECOND_LOWER="Second class Lower Division";
	public static final String CLASS_NORMAL="Normal Pass";
	public static final String CLASS_FAIL="Fail";
	//
	
	public static Icon getButtonIcon(ICON_TYPE it)
	{
		switch(it)
		{
			case ERROR:
				return UIManager.getIcon("OptionPane.errorIcon");	
			case INOFRMATION:
				return UIManager.getIcon("OptionPane.informationIcon");	
			case NOTICE:
				return UIManager.getIcon("OptionPane.warningIcon");
			case QUESTION:
				return UIManager.getIcon("OptionPane.questionIcon");				
			default:
				return UIManager.getIcon("OptionPane.informationIcon");
		}
	}
	
	public static String buildMessageItem(String m)
	{
		return "[>] " + m+"\n";
	}
		
	public static String getMD5(String s) throws Exception
	{		
		MessageDigest m=MessageDigest.getInstance("MD5");
		m.update(s.getBytes(),0,s.length());
		return new BigInteger(1,m.digest()).toString(16);								
	}
	public static String senatizeData(String s)
	{
		return s.replaceAll("[^a-zA-Z0-9\\s-]", "");
	}
	
	public static String getCourseTypeString(COURSE_TYPE s)
	{
		switch(s)
		{
			case CORE: return "CORE";
			case OPTIONAL: return "OPTIONAL";
			case PROJECT: return "PROJECT";
			default: return "OPTIONAL";
		}
	}
	public static COURSE_TYPE getCourseTypeEnum(String s)
	{
		if(s.toUpperCase().equals("CORE"))
		{
			return COURSE_TYPE.CORE;
		}
		else if(s.toUpperCase().equals("OPTIONAL"))
		{
			return COURSE_TYPE.OPTIONAL;
		}
		else
		{
			return COURSE_TYPE.PROJECT;
		}
	}
	
	public static String getStatusString(STATUS s)
	{
		switch(s)
		{
			case ENABLED: return "ENABLED";
			case DISABLED: return "DISABLED";
			default: return "DISABLED";
		}
	}
	public static STATUS getStatusEnum(String s)
	{
		if(s.toLowerCase().equals("enabled"))
		{		
			return STATUS.ENABLED;
		}
		else if(s.toLowerCase().equals("disabled"))
		{
			return STATUS.DISABLED;
		}
		else
		{
			return STATUS.DISABLED;
		}
	}
	
	public static String booleanToString(boolean b)
	{
		return b?TRUE:FALSE;
	}
	
	public static boolean stringToBoolean(String s)
	{
		return s.toLowerCase().equals(TRUE)?true:false;
	}	
	//Validate functions
	public static boolean IsEmptyString(String s)
	{
		return s.trim().equals("");
	}
	
	public static boolean IsCorrectAget(String s)
	{
		int i=0;
		if(IsEmptyString(s))
		{
			return false;
		}
 
		if(isPositiveNumeric(s)==false)
		{
			return false;			
		}
		i=Integer.parseInt(s); 
		if(i< MINIMUM_AGE || i>MAX_AGE)
		{
			return false;
		}
		
		return true;
	}
	public static boolean isPositiveNumeric(String s)
	{
		try  
		{  
			return Integer.parseInt(s) > 0;  			
		}  
		catch(NumberFormatException nfe)  
		{  
			return false;  
		} 
	}
	public static boolean isPositiveWithNumeric(String s)
	{
		try  
		{  
			return Integer.parseInt(s) >=0;  			
		}  
		catch(NumberFormatException nfe)  
		{  
			return false;  
		} 
	}	
	//http://doctordooley.blogspot.com/2009/01/java-regex-validation-snippets.html
	public static boolean isPhoneNumberValid(String phoneNumber)
	{
		boolean isValid = false;
		
		//Initialize reg ex for phone number. 
		String expression = "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$";
		CharSequence inputStr = phoneNumber;
		Pattern pattern = Pattern.compile(expression);
		Matcher matcher = pattern.matcher(inputStr);
		if(matcher.matches())
		{
			isValid = true;
		}
		return isValid;
	}
	
	public static String buildCourseListItem(Course c)
	{
		return Integer.toString(c.getCode())+DEFAULT_DELIMETER+c.getName();
	}	
	public static int getCourseNoFromListItem(String s)
	{
		String [] r=s.split(DEFAULT_DELIMETER);
		return Integer.parseInt(r[0]);
	}
	
	public static String buildBatchListItem(Batch b)
	{
		return Integer.toString(b.getBatchCode())+DEFAULT_DELIMETER+b.getBatchDesc();
	}	
	public static int getBatchNoFromListItem(String s)
	{		
		return getCourseNoFromListItem(s);
	}
	
	public static void createDir(String dirName) {	
		File dir = new File(dirName);
		if (dir.exists()==false) 
		{
			dir.mkdir();
		}
	}	
	
	public static boolean validateNic(String s)
	{
		s=s.toLowerCase();
		if(s.length() != 10)
		{

			return false;
		}
		
		char[] charArray = s.toCharArray();
		for(int i=0;i<s.length()-1;i++)
		{
			if(isPositiveWithNumeric(Character.toString(charArray[i]))==false)
			{

				return false;
			}
		}
		
		if(charArray[s.length()-1]!='x' && charArray[s.length()-1]!='v')
		{

			return false;
		}
		return true;
	}
}